import axios from "../../utils/axios";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    platform: [],
    couponList: [],
    loaderShow: true,
    checkIndex: 0,
    navIndex: 1,
    useState: '立即使用',
    translate: 'transform:translateX(17%)',
    modelShow: false, //模态框是否显示
    cdkey: '', // 兑换码
    couponState: false,
    allCouponList: [],
    remainCount: 0, // 剩下可领取的优惠券
    getShow: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("跳转带来的参数==", options)
    if (options.show) {
      this.setData({
        getShow: options.show
      })
    }
  },

  // 查询优惠券
  selectAllCoupon: function () {
    axios('/groupon/coupons').then(res => {
      if (res.data.resultcode == 1) {

        let list = res.data.result.store
        let itemJson = []

        let notJson = []

        if (list != undefined && list.length > 0) {
          for (let i = 0; i < list.length; i++) {
            if (list[i].receive_status == 1) {
              itemJson.push(list[i])
            } else {
              notJson.push(list[i])
            }
          }
        }

        let tempList = itemJson.concat(notJson)

        // itemJson的长度大于0，那么就是还有可以领取的优惠券
        if (itemJson.length > 0 && this.data.getShow == '') {
          this.setData({
            couponState: true
          })
        } else {
          this.selectCuopon()
        }

        this.setData({
          allCouponList: tempList
        })

      }
    })
  },

  // 点击关闭优惠券弹框
  closeCoupon: function () {
    this.setData({
      couponState: false
    })
    // 查找用户优惠券
    this.selectCuopon()
  },

  // 阻止优惠券模态框的事件冒泡，此方法不能删
  stopCoupon: function () {},

  // 查找优惠券
  selectCuopon() {

    axios('/personal/coupons').then(res => {
      let dataList = res.data.result.list
      if (dataList == undefined) {
        dataList = []
      }
      this.setData({
        platform: dataList,
        couponList: dataList,
        loaderShow: false
      })
    })

  },
  selectRules(e) {
    let index = e.currentTarget.dataset.id
    if (this.data.checkIndex == index) {
      this.setData({
        checkIndex: 0
      })
    } else {
      this.setData({
        checkIndex: index
      })
    }
  },
  // 兑换码输入事件
  cdkeyChange(e) {
    let val = e.detail.value
    this.setData({
      cdkey: val
    })
  },
  // 显示模态框
  showModel() {
    this.setData({
      modelShow: true
    })
  },
  // 关闭模态框
  closeModel() {
    this.setData({
      modelShow: false
    })
  },
  // 点击兑换
  conversion() {
    if (this.data.cdkey == '') {
      wx.showToast({
        title: '请输入兑换码！',
        icon: 'none'
      })
      return
    }
    let that = this

    axios('/personal/convertCoupon', {
      code: that.data.cdkey
    }).then(res => {
      wx.showToast({
        title: res.data.message,
        icon: 'none'
      })
      if (res.data.resultcode == 1) {
        that.selectCuopon()
      }
    })

  },
  typeSwitch(e) {
    let index = parseInt(e.currentTarget.dataset.index),
      that = this,
      useState = "",
      navIndex = 0,
      width = 0,
      couponList = [];

    for (let i = 0, l = that.data.couponList.length; i < l; i++) {
      let clickIndex = parseInt(that.data.couponList[i].status)
      if (index == clickIndex) {
        couponList.push(that.data.couponList[i])
      }
    }

    if (index == 1) {
      navIndex = 1
      width = 17
      useState = "立即使用"
    } else if (index == 2) {
      navIndex = 2
      width = 47
      useState = "已使用"
    } else {
      navIndex = 3
      width = 77

      useState = "已过期"
    }

    that.setData({
      translate: `transform:translateX(${width}%)`,
      useState: useState,
      navIndex: navIndex,
      platform: couponList
    })

  },
  skipIndex() {
    if (this.data.navIndex == 1) {
      wx.switchTab({
        url: '/pages/index/index'
      })
    }

  },

  getcoupons: function () {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.selectAllCoupon()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    let share = {
      title: '查看优惠券',
      success: function (res) {
        // 转发成功
        console.log("转发成功", res)
      },
      fail: function (err) {
        // 转发失败
        console.log("转发失败", err)
      }
    }
    return share
  }
})